package com.ggh.jinjilive.ui.start;

import android.content.Intent;
import android.os.Bundle;


import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.databinding.ActivityStartBinding;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.TotalPageActivity;
import com.ggh.jinjilive.ui.login.LoginActivity;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.loginIMUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMManager;

import static com.tencent.imsdk.v2.V2TIMManager.V2TIM_STATUS_LOGINED;

public class StartActivity extends BaseActivity<ActivityStartBinding> {

    @Override
    public void initCreate() {
        super.initCreate();
        setTheme(R.style.NoStatusTheme1);
        ImmersionBar.with(this).statusBarDarkFont(true).navigationBarColor(R.color.white).navigationBarDarkIcon(true).init();
    }


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_start;

    }

    @Override
    public void initView() {
        super.initView();
        int LoginStatus = V2TIMManager.getInstance().getLoginStatus();

        //跳转到登录页面
        if(MerchantSession.getInstance().isLogin()){
            if(LoginStatus == V2TIM_STATUS_LOGINED){

                Intent intent =new Intent(mContext, TotalPageActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);//启动Activity
                ARouter.getInstance().build(ARouterUtils.TotalPageActivity).navigation();
                finish();
            }else {
                String userSig = loginIMUtils.genTestUserSig(userMobile.getUser_id());
                loginIMUtils.loginIM(userMobile.getUser_id(), userSig, new V2TIMCallback() {
                    @Override
                    public void onSuccess() {
                        Intent intent =new Intent(mContext, TotalPageActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);//启动Activity
                        finish();
                    }

                    @Override
                    public void onError(int code, String desc) {
                        LogUtils.d(code + "IM登录失败" + desc);
                        //标记登录
                        MerchantSession.getInstance().setIsLogin(false);
                    }
                });
            }
        }else {
            Intent intent =new Intent(mContext, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("wherever",true);
            startActivity(intent);//启动Activity
            finish();
        }
    }
}
